package com.example.shoppingcart;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.shoppingcart.database.ShoppingCartDBHelper;
import com.example.shoppingcart.entity.GoodsInfo;

import java.util.ArrayList;

public class SQLiteCrud extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sqlite_crud);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        ShoppingCartDBHelper dbHelper = ShoppingCartDBHelper.getInstance(this);
        SQLiteDatabase readLink = dbHelper.openReadLink();
        SQLiteDatabase writeLink = dbHelper.openWriteLink();

        dbHelper.insertGoodsInfos( GoodsInfo.getDefaultList());
    }

    public void test(){
        // 创建数据库助手实例
        FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getApplicationContext());
        // 获取写模式数据库
        SQLiteDatabase dbWriter = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, "Android开发");
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE, "入门到入土");
        // 插入数据
        dbWriter.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
        // 关闭数据库连接
        dbWriter.close();

        // 获取读模式数据库
        SQLiteDatabase dbReader = dbHelper.getReadableDatabase();
        // 指定列字段
        String[] projection = {
                BaseColumns._ID,
                FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
                FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE
        };

        // Filter results WHERE "title" = 'My Title'
        String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + " = ?";
        String[] selectionArgs = {"Android开发"};

        // results sorted
        String sortOrder =
                FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";

        Cursor cursor = dbReader.query(
                FeedReaderContract.FeedEntry.TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                null
        );

        while (cursor.moveToNext()){
            String title = cursor.getString(
                    cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE)
            );

            Log.d("zhoutao", "title: "+title);

        }
        cursor.close();
        dbReader.close();
    }
}